//
//  CSBAISweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/5.
//

import UIKit
import Alamofire
import JDStatusBarNotification
class CSBAISweeping: UIViewController {
    private var csbColoter:UIColor = .brown
    private var csbFplowe:CGFloat = 23.1
    private var csbHeihei:UIFont?
    
    
    var diChatAICSB:Array<(Bool,String)> = [(false,"Hi,I'm Coach AI")]
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.aiChatCSBDAtaSongView.layer.cornerRadius = 20
        self.aiChatCSBDAtaSongView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        self.chatBottomBg.layer.cornerRadius = 20
        self.chatBottomBg.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        
        
        BoTTomSurePayView.layer.cornerRadius = 20
        BoTTomSurePayView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        
        
 
    }
    
    @IBOutlet weak var BoTTomSurePayView: UIView!
    
    @IBOutlet weak var chatBottomBg: UIView!
   
    @IBOutlet weak var inpuTexfCSB: UITextField!
    
    @IBOutlet weak var aiChatCSBDAtaSongView: UITableView!
    
    
    
    
    @IBAction func comfireSureCSBKIO(_ sender: UIButton) {
        var mineCoint =  Int(CSBFetcherAll.Hammer.melingCSBINfo["allCoind"] ?? "0") ?? 0
        if mineCoint < 100 {
            
            self.navigationController?.pushViewController(CSChoiPayuSweeping.init(), animated: true)
            
        }else{
            self.BoTTomSurePayView.isHidden = true
            mineCoint -= 100
            CSBFetcherAll.Hammer.melingCSBINfo["allCoind"] = "\(mineCoint)"
        }
       
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        inpuTexfCSB.leftViewMode = .always
        inpuTexfCSB.leftView = UIView.init(frame: CGRect.init(x: 0, y:0, width: 40, height: 44))
        aiChatCSBDAtaSongView.rowHeight = UITableView.automaticDimension
        aiChatCSBDAtaSongView.contentInset = UIEdgeInsets.zero
        
        aiChatCSBDAtaSongView.estimatedRowHeight = 70
        aiChatCSBDAtaSongView.register(CSBAIrChatCell.self, forCellReuseIdentifier: "CSBAIrChatCellID")
        aiChatCSBDAtaSongView.separatorStyle = .none
        aiChatCSBDAtaSongView.allowsSelection = false
        aiChatCSBDAtaSongView.showsVerticalScrollIndicator = false
    
        aiChatCSBDAtaSongView.dataSource = self
        aiChatCSBDAtaSongView.delegate = self
        if csbColoter == UIColor.black {
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe)
            self.view.backgroundColor = csbColoter
        }else{
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe + 1)
        }
        guard csbHeihei != nil else { return  }

        if self.view.backgroundColor == csbColoter {
            let qurk = UIButton(configuration: UIButton.Configuration.plain())
            qurk.isHidden = true
            qurk.isEnabled = false
            self.view.addSubview(qurk)
        }

        inpuTexfCSB.layer.cornerRadius = 22
        inpuTexfCSB.layer.masksToBounds = true
        
        
        
    }

   
 
    @IBAction func AllBackCSBClick(_ sender: UIButton) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.popViewController(animated: true)
        
    }
    
    
    
    
    @IBAction func sendMesgCSBClick(_ sender: UIButton) {
        if let conted = inpuTexfCSB.text, conted.count != 0 {
            
            let diologdata = (true,conted)
            diChatAICSB.append(diologdata)
            
            
            inpuTexfCSB.text = nil
            inpuTexfCSB.resignFirstResponder()
             
            askAiAbountPLauerCSB(questingCSB:conted)
            
            self.aiChatCSBDAtaSongView.reloadData()
            
            return
            
        }
        NotificationPresenter.shared.present("Curling AI cannot receive blank questions", includedStyle: .warning)
        
       
    }
    
    
    
    @IBAction func csbReportClicjk(_ sender: UIButton) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.pushViewController(CSBAIrAlertSweeping.init(), animated: true)
    }
    
    func askAiAbountPLauerCSB(questingCSB:String) {
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
      
        guard let url = URL(string: "http://lls.guizhouyezimediacultrue.com/askQuestionv2") else {
            return
        }
   
        AF.request("http://ffd.shanxihealthycology.com/talktwo/askQuestionv2", method: .post, parameters: [
            "question": questingCSB,
            "questionType": 1,
            "eqNo":"5555"
        ], encoding: JSONEncoding.default, headers: nil)
            .responseJSON { response in

                NotificationPresenter.shared.dismiss()
              
                switch response.result {
                case .success(let value):
                    if let json = value as? [String: Any] {
                        guard let content = json["data"] as? String else {

                            return
                        }
                        let diologdata =  (isMine:false,comment:content)
                        
                        self.diChatAICSB.append(diologdata)
                
                        self.aiChatCSBDAtaSongView.reloadData()

                        self.aiChatCSBDAtaSongView.scrollsToTop = true
                           
                        } else {
                            NotificationPresenter.shared.present("Request error occurred.....", includedStyle: .warning)

                        }
                              
                    
                case .failure(let error):
                    NotificationPresenter.shared.present(error.localizedDescription, includedStyle: .warning)
                }
                
            }
        
        
    }
}


extension CSBAISweeping:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return diChatAICSB.count
        }
        return diChatAICSB.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let csbCell = tableView.dequeueReusableCell(withIdentifier:"CSBAIrChatCellID") as! CSBAIrChatCell
        
        let meige = self.diChatAICSB[indexPath.row]
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return csbCell
        }
        csbCell.mychatContetnCSBLab.text = meige.1
        csbCell.updateUIREmaklCSB(ismeCSB: meige.0)
     
        return csbCell
    }
    
    
   
    
}
